package com.junmo.oauth.module.system.controller;

import com.junmo.oauth.config.MyUserDetailService;
import com.junmo.oauth.module.system.entity.SysResource;
import com.junmo.oauth.module.system.entity.SysUser;
import com.junmo.oauth.module.system.service.SysUserService;
import com.junmo.oauth.utils.JwtTokenUtil;
import com.junmo.oauth.utils.R;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Set;

@RestController
@RequestMapping("/auth")
@Slf4j
@AllArgsConstructor
public class SysAuthController {
    private final JwtTokenUtil jwtTokenUtil;
    private final SysUserService sysUserService;
    private final MyUserDetailService myUserDetailsService;
    @GetMapping("/info")
    public R getUserInfo(String token){
        String username = jwtTokenUtil.getUsernameFromToken(token);
        SysUser user = sysUserService.getUserInfo(username);
        return R.success(user);
    }

    @GetMapping("/menu")
    public R getMenu(String token){
        String username = jwtTokenUtil.getUsernameFromToken(token);
        SysUser user = sysUserService.getUserInfo(username);
        Set<SysResource> resources = new HashSet<>();
        user.getRoles().forEach(role->{
            resources.addAll(role.getResources());
        });
        return R.success(resources);
    }

    @GetMapping("/s2")
    public R getMenu(){

        return R.success("sssss");
    }
}
